<template>
  <el-dialog
    :model-value="show"
    title="编辑"
    width="800px"
    append-to-body
    :before-close="handleClose"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    class="form-label-120"
    destroy-on-close
  >
    <DynamicsForm
      ref="DynamicsFormRef"
      :api="companyResourceFb_accountDetail"
      :params="formParams"
    />
    <template #footer>
      <el-button
        type="primary"
        :loading="submitLoading"
        :disabled="!show"
        @click="sureSave"
      >
        保存
      </el-button>
      <el-button type="info" @click="handleClose">取消</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import { companyResourceFb_accountDetail } from '@/api/resources';
import DynamicsForm from '@/components/DynamicsForm';

const props = defineProps({
  show: {
    type: Boolean,
    default: false
  },
  activeRow: {
    type: Object,
    default: () => {}
  }
});
const emit = defineEmits(['reload', 'update:show']);
const formParams = computed(() => {
  return { id: props.activeRow.id, view_type: 2 };
});

// 关闭
function handleClose() {
  emit('update:show', false);
}

// 保存
const submitLoading = ref(false);
const DynamicsFormRef = ref();
async function sureSave() {
  try {
    submitLoading.value = true;
    const values = await DynamicsFormRef.value.returnData();
    companyResourceFb_accountUpdate_put({ values })
      .then(() => {
        handleClose();
        emit('reload');
      })
      .finally(() => (submitLoading.value = false));
  } catch {
    submitLoading.value = false;
  }
}
// 初始化表单
watch(
  () => props.show,
  async val => {
    if (!val) return;
    await nextTick();
    DynamicsFormRef.value?.initFormList();
  }
);
</script>

<style lang="scss" scoped>
.refresh-btn {
  font-weight: bold;
  padding: 0;
  font-size: 20px;
  margin-left: 5px;
}
</style>
